﻿<div id="febs-main-subject" class="layui-row layui-col-space8 febs-container">
    <div class="layui-col-md7 layui-col-sm6 layui-col-xs12">
        <div class="layui-card">
            <div class="layui-card-body febs-table-full">
                <form class="layui-form layui-table-form" lay-filter="main-subject-table-form" id="main-subject-table-form">
                    <div class="layui-row">
                        <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                            <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain  table-action" id="subject-reset">
                                <i>刷新</i>
                            </div>
                            <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain  table-action" id="subject-add">
                                <i>新增</i>
                            </div>
                        </div>
                    </div>
                </form>
                <table lay-filter="mainSubjectTable" lay-data="{id: 'mainSubjectTable'}"></table>
            </div>
        </div>
    </div>
    <div class="layui-col-md5 layui-col-sm6 layui-col-xs12">
        <div class="layui-card">
            <div class="layui-card-header" id="form-header-subject">新增主科目</div>
            <div class="layui-card-body febs-table-full">
                <form class="layui-form layui-table-form" action="" lay-filter="main-subject-form">
                    <div class="layui-form-item febs-hide">
                        <label class="layui-form-label febs-form-item-require">主科目ID：</label>
                        <div class="layui-input-block">
                            <input type="text" name="id" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label febs-form-item-require">主科目名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="mainSubjectName" autocomplete="off" class="layui-input" minlength="2"
                                   maxlength="10" lay-verify="range">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">主科目描述：</label>
                        <div class="layui-input-block">
                            <textarea name="remark" maxlength="50" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item febs-hide">
                        <button type="reset" class="layui-btn" id="reset-form-subject"></button>
                        <button class="layui-btn" lay-submit="" lay-filter="main-subject-form-submit"
                                id="submit-form-subject"></button>
                    </div>
                </form>
            </div>
            <div class="layui-card-footer">
                <button class="layui-btn" id="subject-submit">保存</button>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="main-subject-option">
    <span shiro:lacksPermission="project:update,project:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="edit" shiro:hasPermission="project:update"><i class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del" shiro:hasPermission="project:delete"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdownMenu', 'jquery', 'laydate', 'febs', 'form', 'eleTree', 'table', 'validate'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            validate = layui.validate,
            $view1 = $('#febs-main-subject'),
            $reset1 = $view1.find('#subject-reset'),
            $submit1 = $view1.find('#subject-submit'),
            $header1 = $view1.find('#form-header-subject'),
            $addSubject = $view1.find('#subject-add'),
            tableIns;

        form.verify(validate);
        form.render();

        initTable();

        laydate.render({
            elem: '#createTime',
            range: true
        });

        table.on('tool(mainSubjectTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'edit') {
                $header1.text('修改主科目');
                form.val("main-subject-form", {
                    "id": data.id,
                    "mainSubjectName": data.subjectName,
                    "remark": data.remark
                });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除主科目', '确定删除该主科目？', function () {
                    deleteMainSubject(data.id);
                });
            }
        });

        $reset1.on('click', function () {
            resetMainSubjectForm();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $addSubject.on('click', function () {
            resetMainSubjectForm();
            febs.alert.info("请在表单中填写相关信息");
        });

        $submit1.on('click', function () {
            console.log("提交保存按钮");
            $view1.find('#submit-form-subject').trigger('click');
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view1.find('table'),
                id: 'mainSubjectTable',
                url: ctx + 'subject/list/' + POPUP_DATA.projectId,
                cols: [[
                    {field: 'subjectName', title: '主科目名称', minWidth: 120},
                    {field: 'remark', title: '主科目描述'},
                    {field: 'createTime', title: '创建时间', minWidth: 180},
                    {title: '操作', toolbar: '#main-subject-option', width: 100}
                ]]
            });
        }

        function getQueryParams() {
            let params = {}
            params.invalidate_ie_cache = new Date();
            return params;
        }

        function resetMainSubjectForm() {
            $view1.find('#reset-form-subject').trigger('click');
            $header1.text('新增主科目');
        }

        form.on('submit(main-subject-form-submit)', function (data) {
            addOrUpdateMainSubject(data.field);
            return false;
        });

        function deleteMainSubject(companyIds) {
            febs.get(ctx + 'subject/delete/' + companyIds, null, function () {
                febs.alert.success('删除主科目成功');
                $reset1.trigger('click');
            })
        }

        let addOrUpdateMainSubject = function (data) {
            data.subjectName = data.mainSubjectName;
            data.projectId = POPUP_DATA.projectId;
            if (data.id && $header1.text() === '修改主科目') {
                febs.post(ctx + 'subject/update', data, function () {
                    febs.alert.success('修改主科目成功');
                    $reset1.trigger('click');
                });
            } else {
                febs.post(ctx + 'subject', data, function () {
                    febs.alert.success('新增主科目成功');
                    $reset1.trigger('click');
                });
            }
        }
    });
</script>>